package com.eoner.baselibrary.utils;

import android.text.TextUtils;
import android.widget.TextView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * 作者：sean
 * 创建时间：2017/8/8 14:11
 * 功能说明：
 */

public class TimeUtil {

    static SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static  SimpleDateFormat fmt=new SimpleDateFormat("yyyy-MM-dd");
    static  SimpleDateFormat fmt2=new SimpleDateFormat("MM月dd日 HH:mm:ss");

    /**
     * 传入的时间是否在当前时间之前
     * @param time
     * @return
     */
    public static boolean isBeforeNow(String time){
        Date date = null;
        try {
            date = formatter1.parse(time);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        if(date != null){
            calendar.setTime(date);
        }
        return Calendar.getInstance().getTimeInMillis() > calendar.getTimeInMillis();
    }

    /**
     * 传入的时间是否在当前时间之前
     * @param time
     * @return
     */
    public static boolean isBeforeNow(String time,String nowTime){
        Date date = null;
        Date dateNow = null;
        try {
            date = formatter1.parse(time);
            dateNow = formatter1.parse(nowTime);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        Calendar calendarNow = Calendar.getInstance();
        if(date != null){
            calendar.setTime(date);
        }
        if(dateNow != null){
            calendarNow.setTime(dateNow);
        }
        return calendarNow.getTimeInMillis() > calendar.getTimeInMillis();
    }

    /**
     * 传入时间距当前还剩多少时间
     * @param time
     * @return
     */
    public static long timeRemain(String time){
        Date date = null;
        try {
            date = formatter1.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return Calendar.getInstance().getTimeInMillis() - calendar.getTimeInMillis();
    }

    /**
     * 当前距传入时间还剩多少时间
     * @param time
     * @return
     */
    public static long timeRemain2(String time){
        Date date = null;
        try {
            date = formatter1.parse(time);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        if(date != null){
            calendar.setTime(date);
        }
        return calendar.getTimeInMillis() - Calendar.getInstance().getTimeInMillis();
    }

    /**
     * 当前距传入时间还剩多少时间
     * @param time
     * @return
     */
    public static long timeRemain2(String time,String nowTime){
        Date date = null;
        Date dateNow = null;
        try {
            date = formatter1.parse(time);
            dateNow = formatter1.parse(nowTime);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        Calendar calendarNow = Calendar.getInstance();
        if(date != null){
            calendar.setTime(date);
        }
        if(dateNow != null){
            calendarNow.setTime(dateNow);
        }
        return calendar.getTimeInMillis() - calendarNow.getTimeInMillis();
    }

    public static boolean isTomorrow(String time){
        Date date = null;
        try {
            date = formatter1.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Calendar calendar3 = Calendar.getInstance();
        calendar3.setTime(date);
        calendar3.add(Calendar.DAY_OF_MONTH,1);

        //明天晚上12点
        Calendar calendar1 = Calendar.getInstance();
        calendar1.add(Calendar.DAY_OF_MONTH,1);
        calendar1.set(Calendar.HOUR,23);
        calendar1.set(Calendar.MINUTE,59);
        calendar1.set(Calendar.SECOND,59);
        calendar1.set(Calendar.MILLISECOND,999);
        //明天早上0点
        Calendar calendar2 = Calendar.getInstance();
        calendar2.add(Calendar.DAY_OF_MONTH,1);
        calendar2.set(Calendar.HOUR,0);
        calendar2.set(Calendar.MINUTE,0);
        calendar2.set(Calendar.SECOND,0);
        calendar2.set(Calendar.MILLISECOND,0);

        if(calendar2.getTimeInMillis()<calendar3.getTimeInMillis()&&calendar3.getTimeInMillis()<calendar1.getTimeInMillis()){
            return true;
        }else {
            return false;
        }
    }

    public static boolean isSameDay(String timeCurrent,String time){
        if (TextUtils.isEmpty(time)){return false;}
        java.sql.Date dateCurrent = new java.sql.Date(Long.parseLong(timeCurrent));
        java.sql.Date date = new java.sql.Date(Long.parseLong(time));
        if(fmt.format(date).toString().equals(fmt.format(dateCurrent).toString())){
            return true;
        }else {
            return false;
        }
    }


    /*
 * 毫秒转化时分秒毫秒
 */
    public static Map<String,Long> formatTime(Long ms) {
        Map<String,Long> map = new HashMap<>();
        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;
        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;

        map.put("day",day);
        map.put("hour",hour);
        map.put("minute",minute);
        map.put("second",second);
        return map;
    }


    public static String getShowTimeStr(long ms) {
        Map<String, Long> map = new HashMap<>();
        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;
        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        String time = "";
        if (day > 0) {
            time += (String.format("%02d", day) + "天");
        }
        time += String.format("%02d", hour) + ":" + String.format("%02d", minute) + ":" + String.format("%02d", second) + "";
        return time;
    }

    public static void showTimeWithMsStr(long ms, TextView tvHour,TextView tvMilliSecond) {
        Integer ss = 1000;
        Integer s1 = 10;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;
        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        Long second1 = (ms - day * dd - hour * hh - minute * mi- second*ss) / s1;
        String time = "";
        if (day > 0) {
            time += (String.format("%02d", day) + "天");
        }
        time += String.format("%02d", hour) + ":" + String.format("%02d", minute) + ":" + String.format("%02d", second) + "."
                + String.format("%02d", second1);
        tvHour.setText(day*24+hour+" 时 "+minute+" 分");
        tvMilliSecond.setText(String.format("%02d", second) + "." + String.format("%02d", second1));
    }

    public static String formatTime2(String time){
        Date date = null;
        try {
            date = formatter1.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }

       return fmt2.format(date);
    }

    //返回hh:mm:ss格式,ms是秒
    public static String getTimeString(Long ms) {
        Integer ss = 1;
        Integer mi = ss * 60;
        Integer hh = mi * 60;

        Long hour = ms / hh;
        Long minute = (ms - hour * hh) / mi;
        Long second = (ms - hour * hh - minute * mi) / ss;
        String time = "";
        time += String.format("%02d", hour) + ":" + String.format("%02d", minute) + ":" + String.format("%02d", second);
        return time;
    }

    public static long strToLong(String time) throws ParseException {
        Date date = formatter1.parse(time);
        Long timestamp = date.getTime();
        return  timestamp;
    }


    public static String getTimeStringAftersale(Long ms) {
        Map<String, Long> map = new HashMap<>();
        Integer ss = 1;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;
        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        String time = "";
        if (day > 0) {
            time += (String.format("%02d", day) + "天");
        }
        time += String.format("%02d", hour) + "时" + String.format("%02d", minute) + "分" + String.format("%02d", second) + "秒";
        return time;
    }

    public static String get_current_time(){
        Date date = new Date();
        return formatter1.format(date);
    }

    public static float time_remain_day(String start,String end){
        float days = 0f;
        try {
            Date startTime = formatter1.parse(start);
            Date endTime = formatter1.parse(end);
            long diff = endTime.getTime() - startTime.getTime();
            days = diff / (1000 * 60 * 60 * 24);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return days;
    }

    public static String getDate2String(long time, String pattern) {
        Date date = new Date(time);
        SimpleDateFormat format = new SimpleDateFormat(pattern, Locale.getDefault());
        return format.format(date);
    }

    public static void orderBtnCountdown(TextView tv, long seconds){
        long min = seconds / 60;
        long sec = seconds - 60 * min;
        tv.setText( (String.valueOf(min).length()>1?min:("0"+min)) + ":" + (String.valueOf(sec).length()>1?sec:("0"+sec)));
    }

    public static String formatTime(long time, String pattern){
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        Date d1 = new Date(time);
        return format.format(d1);
    }

    public static String formatTime(String time)throws ParseException {
        Date date = new Date(strToLong(time));
        return fmt.format(date);
    }
}
